/*
 *Copyright (c) 2024 Black Sesame Technologies
 *
 *Licensed under the Apache License, Version 2.0 (the "License");
 *you may not use this file except in compliance with the License.
 *You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 *Unless required by applicable law or agreed to in writing, software
 *distributed under the License is distributed on an "AS IS" BASIS,
 *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *See the License for the specific language governing permissions and
 *limitations under the License.
*/
#include "VCan_Command.h"
#include "eth2can.h"
#include "hscg_firmware_statistic.h"
#include "lwrb/lwrb.h"
#include "sq_buffer/sq_buffer.h"
#include "vcan.h"

/* ************************************msgbox相关全局变量   START************************/
switch0_canoe_ipc_client_t *can_client;
switch0_can_gateway_ipc_server_t *can_s_server;
static switch0_can_gateway_ipc_server_data_t server_data = {0};
static switch0_canoe_ipc_client_data_t can_client_data = {0};
/* ************************************msgbox相关全局变量   END************************/


extern void eth2can_enqueue_cb(const uint32_t element_ptr, const uint16_t element_count,char **message,
				               void *err,const ext_info_t *info);
extern void eth2can_dequeue_cb(const uint32_t element_ptr, const uint16_t element_count, char **message,
				            void *err, const ext_info_t *info);

int can_ipc_init() 
{
    int32_t ret = 0;

    // 初始化CAN服务器
    can_s_server = switch0_can_gateway_ipc_server_init(&server_data);
    if (!can_s_server) {
        hprintf(DEBUG, "Failed to initialize CAN server.\n");
        return -1;
    }

    // 注册Eth到CAN入队回调
    ret = can_s_server->switch0_can_gateway_server.register_notify_Eth_2_Can_enqueue(&eth2can_enqueue_cb);
    if (ret < 0) {
        hprintf(DEBUG, "Failed to register Eth to CAN enqueue callback.\n");
        return -2;
    }

    // 注册CAN到Eth出队回调
    ret = can_s_server->switch0_can_gateway_server.register_notify_Can_2_Eth_dequeue(&eth2can_dequeue_cb);
    if (ret < 0) {
        hprintf(DEBUG, "Failed to register CAN to Eth dequeue callback.\n");
        return -2;
    }

    // 注册获取ETH统计信息回调
    ret = can_s_server->switch0_can_gateway_server.register_get_ETH_statistics(&get_statistics);
    if (ret < 0) {
        hprintf(DEBUG, "Failed to register ETH statistics callback.\n");
        return -2;
    }

    // 注册虚拟CAN方法回调
    ret = can_s_server->switch0_can_gateway_server.register_vcan_method(&vcan_method_callback);
    if (ret < 0) {
        hprintf(DEBUG, "Failed to register VCAN method callback.\n");
        return -2;
    }

    // 初始化CAN客户端
    can_client = switch0_canoe_ipc_client_init(&can_client_data);
    if (!can_client) {
        hprintf(DEBUG, "Failed to initialize CAN client.\n");
        return -1;
    }

    ret = can_s_server->switch0_can_gateway_server.register_can_gateway_send(&vcan_send);
    if (ret < 0) {
        hprintf(DEBUG, "register register_can_gateway_send error\n");
        return -2;
    }

    ret = can_s_server->switch0_can_gateway_server.register_can_gateway_recv_subcribed(&vcan_read_subcribed);
    if (ret < 0) {
        hprintf(DEBUG, "register register_can_gateway_read_subcribed error\n");
        return -2;
    }

    ret = can_s_server->switch0_can_gateway_server.register_can_gateway_recv_unsubcribed(&vcan_read_unsubcribed);
    if (ret < 0) {
        hprintf(DEBUG, "register register_can_gateway_read_unsubcribed error\n");
        return -2;
    }

    return 0;
}


void can_ipc_run(void)
{
    int ret;

    // 接收msgbox消息
    ret = can_s_server->receive_message();
    if (ret < 0) {
        // hprintf(DEBUG, "Failed to receive message: error code %d\n", ret);
        return;
    }

    // 调度消息
    ret = can_s_server->dispatch_message();
    if (ret < 0) {
        hprintf(DEBUG, "Failed to dispatch message: error code %d\n", ret);
    }
}